package com.dimanche.library_base.baseui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.dimanche.library_base.utils.ThreadPoolUtils;

/**
 * author : Dimanche
 * date   : 2020/9/7 20:17
 * desc   :能够自动消失的对话框
 * version: 1.0
 */
public class BaseAutoCancelDialog extends Dialog {
    private final int sleepTime = 2000;
    Context mContext;

    public BaseAutoCancelDialog(@NonNull Context context) {
        super(context);
        mContext = context;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }


    @Override
    public void show() {
        super.show();
        cancle();
        setWindowSize();
    }

    /**
     * 设置Dialog窗口的大小
     */
    private void setWindowSize() {
        Window window = getWindow();
        WindowManager manager = window.getWindowManager();
        Display d = manager.getDefaultDisplay();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = d.getWidth() / 2;
        params.height = d.getHeight() / 2;
        window.setAttributes(params);
    }

    private void cancle() {
        ThreadPoolUtils.getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        }, sleepTime);
    }


}
